import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal

# 参数
mu = np.array([0, 0])
Sigma = np.array([[1, 0.8], [0.8, 1]])

# 绘制二维等密度轮廓
x, y = np.mgrid[-3:3:.01, -3:3:.01]
pos = np.dstack((x, y))
rv = multivariate_normal(mu, Sigma)

plt.contour(x, y, rv.pdf(pos), levels=10, cmap='coolwarm')
plt.title("2D Gaussian Density Contours")

# 条件 x2=1 时 x1 的条件分布
c = 1
cond_mu = 0.8 * c
cond_sigma = np.sqrt(0.36)

# 画条件分布位置
plt.axvline(x=c, color="black", linestyle="--")
plt.plot(cond_mu, c, 'ro', label="conditional mean")

plt.xlabel("x1")
plt.ylabel("x2")
plt.legend()
plt.show()
